package civicinity.ist.psu.edu.events;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.OverlayItem;

import civicinity.ist.psu.edu.R;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class CivicinityCalFeedDetail extends MapActivity {

	private MyLocationOverlay mMyOverLay = null;
	private MapController mMapController = null;
	private MapView vMap;
	private TextView vTitle;
	private TextView vDescription;
	private TextView vDistance;
	private TextView vDateTime;
	
	private Drawable mMarker;
	private GeoPoint gPoint;
	
	private SharedPreferences mPref;
	
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	
	    setContentView(R.layout.civicinity_cal_detail);
	    
	    mPref = PreferenceManager.getDefaultSharedPreferences(this);
	    
	    Intent gIntent = getIntent();
	    String eTitle = gIntent.getExtras().getString("eTitle");
	    String eDescription = gIntent.getExtras().getString("eDescription");
	    String eStartDate = gIntent.getExtras().getString("eStartDate");
	    String eEndDate = gIntent.getExtras().getString("eEndDate");
	    String eLocation = gIntent.getExtras().getString("eLocation");
	    
	    vMap = (MapView)findViewById(R.id.map);
	    vMap.setBuiltInZoomControls(true);

	    mMyOverLay = new MyLocationOverlay(this, vMap);
		mMyOverLay.enableMyLocation();
	    
	    vTitle = (TextView)findViewById(R.id.title);
	    vDescription = (TextView)findViewById(R.id.description);
	    vDistance = (TextView)findViewById(R.id.distance);
	    vDateTime = (TextView)findViewById(R.id.datetime);
	    
	    vTitle.setText(eTitle);
	    vDescription.setText(eDescription);
	    vDateTime.setText(eStartDate + " ~ " + eEndDate);
	    
	    Log.i("K", "eLocation : " + eLocation);
	    
	    if(eLocation != null) {
	    	if(eLocation.length() > 0) {
	    		
	    		if(Character.isDigit(eLocation.trim().charAt(0))) {
			    	//eLocation = eLocation.replaceAll(",", "");
				    String[] splitLocStr = eLocation.split(",");
			    	
			    	mMarker = getResources().getDrawable(R.drawable.marker);
					mMarker.setBounds(0, 0, mMarker.getIntrinsicWidth(), mMarker.getIntrinsicHeight());
					
					gPoint = new GeoPoint((int) (Double.parseDouble(splitLocStr[0].trim()) * 1000000), (int) (Double.parseDouble(splitLocStr[1].trim()) * 1000000));
					vMap.getController().setCenter(gPoint);
					vMap.getController().setZoom(14);
					
					FeedOverlay overLay = new FeedOverlay(mMarker);
					vMap.getOverlays().add(overLay);
					
					float[] dist = new float[1];
					
					Log.i("K", "latitude : " + Double.parseDouble(mPref.getString("latitude", "0.0")));
					
					Location.distanceBetween(Double.parseDouble(mPref.getString("latitude", "0.0")), Double.parseDouble(mPref.getString("longitude", "0.0")), 
							Double.parseDouble(splitLocStr[0].trim()), Double.parseDouble(splitLocStr[1].trim()), dist);
					
					vDistance.setText(String.format("Distance : %5.1f miles away", dist[0] * 0.0006));
	    		}
	    		else {
	    			vDistance.setText("Location info. is not available.");
	    		}
		    }
	    	else {
	    		vDistance.setText("Location info. is not available.");
		    }
	    }
	    else {
	    	vDistance.setText("Location info. is not available.");
	    	//Toast.makeText(this, "There is no Geo-information on this event", Toast.LENGTH_SHORT).show();
	    	//vMap.setVisibility(View.GONE);
	    }

	    
	    // TODO Auto-generated method stub
	}
	
	class FeedOverlay extends ItemizedOverlay {

		private Drawable mMarker;
		//private double mLatitude;
		//private double mLongitude;
		private OverlayItem oItem;
		
		public FeedOverlay(Drawable marker) {
			super(marker);
			mMarker = marker;
			//mLatitude = latitude;
			//mLongitude = longitude;
			
			oItem = new OverlayItem(gPoint, "", "");
			
			populate();
			
			// TODO Auto-generated constructor stub
		}
		
		@Override
		public void draw(Canvas canvas, MapView mapView, boolean shadow) {
			super.draw(canvas, mapView, false);
		}

		@Override
		protected OverlayItem createItem(int arg0) {
			// TODO Auto-generated method stub
			return oItem;
		}

		@Override
		public int size() {
			// TODO Auto-generated method stub
			return 1;
		}
		
	}

	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}
	
	public void onDestroy() {
		super.onDestroy();
		
		mMyOverLay.disableMyLocation();
	}

}
